define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'dept/index' + location.search,
                    add_url: 'dept/add',
                    edit_url: 'dept/edit',
                    del_url: 'dept/del',
                    multi_url: 'dept/multi',
                    import_url: 'dept/import',
                    table: 'dept',
                }
            });

            var table = $("#table");

            // 修改表格搜索框的默认placeholder
            $.fn.bootstrapTable.locales[Table.defaults.locale]['formatSearch'] = function(){return "ID";};

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                showToggle: false,
                searchFormVisible: true,
                clickToSelect: false,
                columns: [
                    [
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'total', title: __('Total'), operate: false},
                        {field: 'ports', title: __('Ports'), operate: false},
                        {field: 'service', title: __('First'), operate: false},
                        {field: 'bottom', title: __('Bottom'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'ports',
                                    text: __('Ports'),
                                    classname: 'btn btn-info btn-addtabs',
                                    url: 'ports?dept_id={ids}',
                                },
                                {
                                    name: 'first',
                                    text: __('First'),
                                    classname: 'btn btn-success btn-addtabs',
                                    url: 'first?dept_id={ids}',
                                },
                                {
                                    name: 'bottom',
                                    text: __('Bottom'),
                                    classname: 'btn btn-primary btn-addtabs',
                                    url: 'bottom?dept_id={ids}',
                                }
                            ],
                            events: Table.api.events.operate,
                            // formatter: Table.api.formatter.operate
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);

                                // 隐藏编辑和删除按钮
                                $(table).data("operate-edit", null);
                                $(table).data("operate-del", null);

                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});